<?php
require_once 'config.php'; 
require_once 'User.class.php'; 
require_once 'Score.class.php'; 
require_once 'WeiChat.class.php'; 
require_once 'Courses.class.php'; 
require_once 'Library.class.php'; 
require_once 'Robot.class.php'; 
require_once 'News.class.php'; 
require_once 'Exam.class.php'; 
require_once 'Tools.class.php'; 
require_once 'HTML.class.php'; 
require_once 'Semantic.class.php'; 

//测试代码块
{
	if($_GET['a']=="test"){
        $str="今天课程表";
		echo Semantic::testAnalyze($str);
		die();
	}
}

$dzu = new SmartDZU_WeiChat();
$dzu->Start();
class SmartDZU_WeiChat{

    //入口函数
    public function Start(){
        if(WeiChat::checkSignature()){
            $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
            if (!empty($postStr)){
                $postObj = WeiChat::getWeiChatXML($postStr);
                $keyword = trim($postObj->Content);//用户发送的内容
                $keyword=str_replace("＃", "#", $keyword);//中英转换
                $from = $postObj->FromUserName;//用户id
                $to = $postObj->ToUserName;//接受id
                $enent=$postObj->Event;//事件类型，subscribe(订阅)、unsubscribe(取消订阅)
                $type=$postObj->MsgType;//event
                if($type=='event'){//事件
                    if($enent=='subscribe'){//关注
            			Tools::SendMMS("18315911332","❤新增关注用户");
                        $str="[微笑]欢迎关注德州学院微信公众平台!";
                        $str.= "请发送12位学号和身份证号后6位(以#号分割)完成绑定。\n如:201211000000#888888";
                        $str.="\n-----------------\n<a href=\"".WEB."\">回复\"0\"或\"菜单\"查看菜单</a>";
                        WeiChat::SendText($from,$to,$str);
                    }elseif ($enent=='unsubscribe') {//取消关注
                        User::setDisable($from);
                        Tools::SendMMS("18315911332","❗用户取消关注[".User::getXM($from)."]");
                    }
                }elseif ($type=='text') {//用户发送文本消息
                    Tools::wxLog($from,$keyword,"send");//消息日志记录
                    $r=User::verBinding($from,$keyword);//检测用户是否绑定
                    if($r=='ok'){
                        $this->menu($from,$to,$keyword);
                    }else{
                        WeiChat::SendText($from,$to,$r);
                    }
                }
            }
        }
    }

	
   //菜单判断
    private function menu($fromUsername,$toUsername,$contentStr){
    	//超级权限判断
    	$SuperPwd1='995800'.date('Hi');
        $SuperPwd2='995801'.date('Hi');
        if($contentStr==$SuperPwd1){
        	$str = "【信息查询】\n"."<a href=\"".API_URL."weixin/web/index.php?action=xxcx&code=".$SuperPwd."&opid=_OPID\">点击此处查询</a>";
            WeiChat::SendText($fromUsername,$toUsername,$str);
            return;
        }
        if($contentStr==$SuperPwd2){
            $str= "【领导邮箱】\n"."<a href=\"".API_URL."weixin/web/index.php?action=maillist&code=".$SuperPwd."&opid=_OPID\">点击此处查看</a>";
            WeiChat::SendText($fromUsername,$toUsername,$str);
            return;
        }

        $temp = explode("#",trim($contentStr));
        $word=Semantic::Analyze($temp[0]);
        switch($word){
            case '课程查询':
            case '1': 
				$str = "【课程查询】\n【<a href=\"".WEB."\">10</a>】今天课程\n【<a href=\"".WEB."\">11</a>】明天课程\n【<a href=\"".WEB."\">12</a>】本周课程";
                WeiChat::SendText($fromUsername,$toUsername,$str);
                break;
            case '成绩查询':
            case '2': 
			    $str = "【成绩查询】\n【<a href=\"".WEB."\">20</a>】上学期成绩\n【<a href=\"".WEB."\">21</a>】本学期成绩\n【<a href=\"".WEB."\">22</a>】自定义查询";
                WeiChat::SendText($fromUsername,$toUsername,$str);
                break;
            case '3': 
                $str = "【考试查询】\n【<a href=\"".WEB."\">30</a>】考试查询";
                WeiChat::SendText($fromUsername,$toUsername,$str);
                break;
            case '图书查询':
            case '4': 
            	$str = "【图书查询】\n【<a href=\"".WEB."\">40</a>】借阅查询\n【<a href=\"".WEB."\">41</a>】归还查询\n【<a href=\"".WEB."\">42</a>】账目查询\n【<a href=\"".WEB."\">43</a>】图书检索";
           		WeiChat::SendText($fromUsername,$toUsername,$str);
                break;
            case '新闻浏览':
            case '5': 
                $str = "【新闻浏览】\n【<a href=\"".WEB."\">50</a>】学院新闻";
                WeiChat::SendText($fromUsername,$toUsername,$str);
                break;
            //课表查询=================================================
            case '今天课程':
			case '10':
                $co = new Courses();
			    $co->setCourses($fromUsername);
        		$str = $co->getTodayClass();
                WeiChat::SendText($fromUsername,$toUsername,$str);
				break;
            case '明天课程':
            case '11':
            	$co = new Courses();
				$co->setCourses($fromUsername);
                $str = $co->getNextdayClass();
                WeiChat::SendText($fromUsername,$toUsername,$str);
				break;
            case '本周课程':
            case '12':
            	$co = new Courses();
				$co->setCourses($fromUsername);
                $str = $co->getWeekClass();
                WeiChat::SendText($fromUsername,$toUsername,$str);
           		break;
            //成绩查询=============================================
            case '上学期成绩':
			case '20':
            	$sc = new Score();
            	$str = $sc->getLast($fromUsername);
                WeiChat::SendText($fromUsername,$toUsername,$str);
				break;
            case '本学期成绩':
            case '21':
                $sc = new Score();
                $str = $sc->getThis($fromUsername);
                WeiChat::SendText($fromUsername,$toUsername,$str);
				break;
            case '自定义查询':
            case '22':
                $str = "【成绩查询】\n"."<a href=\"".API_URL."weixin/web/index.php?action=cjcx&opid=_OPID\">点击此处查询</a>";
                WeiChat::SendText($fromUsername,$toUsername,$str);
                break;
            //考试查询=====================================================
            case '考试查询':
            case '30':
                $sc = new Exam();
                $str = $sc->getExam($fromUsername);
                WeiChat::SendText($fromUsername,$toUsername,$str);
                break;
            //图书查询==========================================================
            case '借阅查询':
            case '40':
                $l = new Library();
                $str = $l->getXJCX($fromUsername);
                WeiChat::SendText($fromUsername,$toUsername,$str);
                break;
            case '归还查询':
            case '41':
                $l = new Library();
                $str = $l->getGHCX($fromUsername);
                WeiChat::SendText($fromUsername,$toUsername,$str);
                break;
            case '账目查询':
            case '42':
                $l = new Library();
                $str = $l->getZMCX($fromUsername);
                WeiChat::SendText($fromUsername,$toUsername,$str);
                break;
            case '图书检索':
            case '43':
                $str = "【图书检索】\n"."<a href=\"".API_URL."weixin/web/index.php?action=tsjs&opid=_OPID\">点击此处检索</a>";
                WeiChat::SendText($fromUsername,$toUsername,$str);
                break;
            case '学院新闻':
            case '50':
                $n=new News();
                $str = $n->getNewsList($fromUsername);
                WeiChat::SendText($fromUsername,$toUsername,$str);
                break;
            //==========================================================
            case '菜单':
            case '帮助':
            case '主菜单':
            case '首页':
            case 'MENU':
            case 'menu':
            case '?':
            case '0':
            case '00':
                $str = MENU;
                WeiChat::SendText($fromUsername,$toUsername,$str);
                break;
            case 'test':
                //WeiChat::SendText($fromUsername,$toUsername,date("YmdHi"));
                break;
            //聊天==============================================
            default:
				$robot=new Robot();
                $str=$robot->chat($contentStr);
                $str.="\n\n-----------------\n<a href=\"".WEB."\">回复\"0\"或\"菜单\"查看菜单</a>";
                WeiChat::SendText($fromUsername,$toUsername,$str);
			}
    }
    
    
}

?>